#!/bin/bash

apt-get install -y gcc g++ make cmake cmake-curses-gui autoconf

apt-get purge needrestart -y

# ffmpeg 安装路径
export ipath=/mnt/nas/work/sdk/ffmpeg
rm -rf $ipath

# 设置安装包执行路径
export PATH=$PATH:$ipath/bin

# 设置动态库路径
export PKG_CONFIG_PATH=$ipath/lib/pkgconfig

# 设置编译主目录路径
export homeMake=$(pwd)




# 1 nasm/yasm not found or too old. Use --disable-x86asm for a crippled build
#  解压 yasm
tar -xvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
# 编译 yasm 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install

# 2 Libnettle 3.6 was not found
apt-get install -y libbluray-dev

# Libhogweed (nettle's companion library) 3.6 was not found. Note that you must compile nettle with gmp support
apt install -y libgnutls28-dev

#  Libunistring was not found. To use the included one, use --with-included-unistring
apt-get install -y libunistring-dev

# 2 ERROR: gnutls not found using pkg-config
# 进入编译主目录路径
cd $homeMake
rm -rf yasm-1.3.0

#  解压  gnutls
tar -xvf gnutls-3.7.0.tar.xz
# 进入  gnutls  目录
cd gnutls-3.7.0
export PKG_CONFIG_PATH=$ipath/lib/pkgconfig:$ipath/lib64/pkgconfig
# 编译 gnutls 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install

# 3 ERROR: libfdk_aac not found
# 进入编译主目录路径
cd $homeMake
rm -rf gnutls-3.7.0

#  解压  fdk-aac
tar -xvf fdk-aac-2.0.3.tar.gz

# 进入  fdk-aac  目录
cd fdk-aac-2.0.3

# 编译 fdk-aac 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install
make distclean

# 4 ERROR: libmp3lame >= 3.98.3 not found
# 进入编译主目录路径
cd $homeMake
rm -rf fdk-aac-2.0.3

#  解压  libmp3lame
tar -xvf lame-3.100.tar.gz

# 进入  libmp3lame  目录
cd lame-3.100

# 编译 libmp3lame 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install


# 5 ERROR: speex not found using pkg-config
# 进入编译主目录路径
cd $homeMake
rm -rf lame-3.100

#  解压  speex
tar -xvf speex-1.2.1.tar.gz

# 进入  speex  目录
cd speex-1.2.1/

# 编译 speex 配置
./configure --prefix=$ipath  --enable-shared --disable-static
make -j4
make install

# 6 ERROR: libx264 not found
# 进入编译主目录路径
cd $homeMake
rm -rf speex-1.2.1

#  解压  x264
tar -xvf x264.tar.gz

# 进入  x264  目录
cd x264

# 编译 x264 配置
./configure --prefix=$ipath  --disable-asm --enable-shared --disable-static
make -j4
make install

# 7 ERROR: x265 not found using pkg-config
# 进入编译主目录路径
cd $homeMake
rm -rf x264
#  解压  x265
tar -xvf x265.tar.gz

# 进入  x265  目录
cd x265/build/linux

# 编译 x265 配置
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$ipath ../../source
make -j4
make install

# 8 编译  ffmpeg

# 进入编译主目录路径
cd $homeMake
rm -rf x265
#  解压  ffmpeg
tar -xvf ffmpeg-4.4.4.tar.gz

# 进入  ffmpeg  目录
cd ffmpeg-4.4.4/

./configure \
    --prefix=$ipath \
	  --enable-shared \
	  --disable-static \
 		--enable-gpl \
 		--enable-nonfree \
 		--enable-gnutls \
 		--enable-libmp3lame \
    --enable-libfdk-aac \
    --enable-libx264 \
    --enable-libx265 \
    --enable-filter=delogo \
    --enable-debug \
    --enable-optimizations \
    --enable-libspeex \
    --enable-pthreads \
    --enable-version3 \
    --enable-hardcoded-tables \
    --extra-cflags="-I$ipath/include"  \
    --extra-ldflags="-L$ipath/lib" \
    --arch=x86_64

make -j4
make install

# 进入编译主目录路径
cd $homeMake
rm -rf ffmpeg-4.4.4
# 配置动态库位置 $ipath
echo "$ipath/lib" >> /etc/ld.so.conf

# 刷新动态库配置
ldconfig
ffmpeg -version
